package com.ssm.service;


import com.ssm.dao.MoneyDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MoneyService {

    @Autowired
    MoneyDao moneyDao;

    @Transactional
    public String transformAccounts(int outId,int inId,int money){

        String backStr = "";

        //从原始账户中转出金额，并获取受影响行数
        int outTransformCount = moneyDao.updateAccountById(outId,false,money);
        //向目标账户中转入金额，并获取受影响行数
        int inTransformCount = moneyDao.updateAccountById(inId,true,money);

        //如果转入或转出的任何一方未成功
        if(outTransformCount!=1 || inTransformCount!=1){
            //向页面返回信息
            backStr = "转账失败";
            //抛出失败异常
            throw new RuntimeException("转账失败");
        }else{
            backStr = "转账成功";
        }
        return backStr;
    }

}
